package com.example.wordWork.servie.impl;

import com.example.wordWork.domain.coupon_inspect;
import com.spire.doc.*;
import com.spire.doc.collections.CellCollection;
import com.spire.doc.collections.RowCollection;
import com.spire.doc.collections.SectionCollection;
import com.spire.doc.collections.TableCollection;
import org.springframework.stereotype.Service;
import com.example.wordWork.servie.inspectTable;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class inspectTableImpl implements inspectTable {
    public static final String docSrc="C:\\Users\\27351\\Desktop\\记录\\试块管理台账地下室最新9.19(1).doc";
    @Override
    public Map<String, Map<String, List<coupon_inspect>>> getZhuLouCoupon_inspectByCoupon_inspectTable() {
        HashMap<String, HashMap<String, List<coupon_inspect>>> map = new HashMap<>();
//        System.out.println("****************");
        Document doc = new Document();
        doc.loadFromFile(docSrc);
        //只有第一节
        Section section = doc.getSections().get(0);
        //获取全部表格
        TableCollection tables = section.getTables();
        Map<String, List<String>> zhuLouBuWeiAllByTables = getZhuLouBuWeiAllByTables(tables);
        for (String zhulou : zhuLouBuWeiAllByTables.keySet()) {
            List<String> list = zhuLouBuWeiAllByTables.get(zhulou);
            for (String s : list) {
                getCoupon_inspectListByBuWei(tables,s);
            }
        }
        return null;
    }

    /**
     * 获取主楼的全部部位 按楼层分
     * @param tables
     * @return
     */
    private Map<String,List<String>> getZhuLouBuWeiAllByTables(TableCollection tables){
        ArrayList<String> list = new ArrayList<>();
        for(int i=0;i<tables.getCount();i++){
            Table table = tables.get(i);
            RowCollection rows = table.getRows();
            for(int k=0;k<rows.getCount();k++){
                String buWei = getCellValue(rows.get(k).getCells().get(1));
                if(checkBuWeiIsZhuLou(buWei)&&!list.contains(buWei))  list.add(buWei);
            }
        }
        HashMap<String, List<String>> map = new HashMap<>();
        for (String buWei : list) {
            String size = buWei.substring(0, buWei.indexOf("#楼")+2);
            if(!map.containsKey(size)) map.put(size,new ArrayList<>());
            map.get(size).add(buWei);
        }
//        System.out.println(map);
        return map;
    }

    /**
     * 获取单元格的值 <br/>
     * 若为""则返回null <br/>
     * 若字符串中存在制表符则返回null
     * @param cell
     * @return
     */
    private String getCellValue(TableCell cell){
        String text = cell.getParagraphs().get(0).getText();
            text=text.replace(" ","");
            for(int i=0;i<text.length();i++){
                int charAt = text.charAt(i);
                if(charAt==12288){return null;}
            }
        return "".equals(text) ?null :text;
    }
    private boolean checkBuWeiIsZhuLou(String buWei){
        if(buWei==null){return false;}
//
        if(!buWei.contains("#楼")){return false;}
        if(!(buWei.contains("墙柱")||buWei.contains("梁板"))){return false;}
        try {
            Integer.parseInt(buWei.substring(0,buWei.indexOf("#楼")));

        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    /**
     * 获取主楼每一个部位的记录集合 注意重复记录
     * @param tables
     * @param buWei
     * @return
     */
    public List<coupon_inspect> getCoupon_inspectListByBuWei(TableCollection tables,String buWei){
        ArrayList<coupon_inspect> list = new ArrayList<>();
        for(int i=0;i<tables.getCount();i++){
            Table table = tables.get(i);
            RowCollection rows = table.getRows();
            for(int k=0;k<rows.getCount();k++){
                TableRow row = rows.get(k);
                CellCollection cells = row.getCells();
                if(!buWei.equals(getCellValue(row.getCells().get(1)))) continue;
                setCoupon_inspect(cells,buWei);
            }
        }
        return list;
    }
    public coupon_inspect setCoupon_inspect(CellCollection cells,String buWei){
        coupon_inspect coupon_inspect = new coupon_inspect();
        if(getCellValue(cells.get(2))==null)return null;  //如果没有制作时间则返回null
        coupon_inspect
                .setBuWei(getCellValue(cells.get(1)))
                .setCreate_date(getCellValue(cells.get(2)))
                .setType(getCellValue(cells.get(3)))
                .setGrade(getCellValue(cells.get(4)))
                .setCapacity(getCellValue(cells.get(5)))
                .setCount(getCellValue(cells.get(6)))
                .setInspect_date(getCellValue(cells.get(7)))
                .setSeal_date(getCellValue(cells.get(8)));
        System.out.println(coupon_inspect);
        return coupon_inspect;
    }
}
